<?php
/**
 * @author 段志岩
 * @date 2008.8.9
 * @summary 模型类
 */

class Model extends Base{
	protected $tableName;
	protected $data;
	protected $db;
	protected $conditions;
	protected $formMapping;

	public function __construct()
	{
		global $db;
		$this->db = &$db;
	}
	public function __destruct()
	{
	}
	public function __get($property)
	{
		if(array_key_exists($property, $this->data))
		{
			return $this->data[$property];
		}
	}
	public function __set($property, $value)
	{
		$this->data[$property] = $value;
	}
	public function excuteSql($sql)
	{
		return $this->db->query($sql);
	}
	public function find()
	{
		$this->data = $this->db->query($this->toSql("select"));
		$this->clearCondition();

		return $this->data;
	}
	public function findAll()
	{
		$this->data = $this->db->query($this->toSql("findAll"));

		return $this->data;
	}
	public function findOne()
	{
		$this->find();
		$this->clearCondition();
		$tmp = $this->data;
		$this->data = null;
		$this->data = $tmp[0];
		return $this->data;
	}
	public function add()
	{
		$r = $this->db->update($this->toSql("insert"));
		$this->clearCondition();
		return $r;
	}
	public function update()
	{
		$r = $this->db->update($this->toSql("update"));
		$this->clearCondition();
		return $r;
	}
	public function delete()
	{
		$r = $this->db->update($this->toSql("delete"));
		$this->clearCondition();
		return $r;
	}
	public function count($field)
	{
		return $this->funSql("count", $field, $group);
	}
	public function avg($field, $group = "")
	{
		return $this->funSql("avg", $field, $group);
	}
	public function sum($field, $group = "")
	{
		return $this->funSql("sum", $field, $group);
	}

	public function funSql($func, $field, $group="")
	{
		$sql = "SELECT $func(`$field`) FROM `". $this->tableName ."` ". $this->parseWhere();
		if($group != "")
		{
			$sql .= " GROUP BY `$group`";
		}

		$this->data = $this->db->query($sql);
		return $this->data[0][0];
	}

	public function create()
	{
		$input = $_POST;
		if(is_array($this->formMapping))
		{
			foreach($this->formMapping as $formField => $dbField)
			{
				if(array_key_exists($formField, $input) && $input[$formField] != "")
				{
					$this->data[$dbField] = $input[$formField];
				}
			}
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 添加查询条件
	 *
	 * @param $field <string> 字段名称
	 * @param $op <string> 操作符 : =, >, >=, <=
	 * @param $value <string> 字段值
	 * @param $logic_op <string> 逻辑连接符，用于与后面的条件进行连接。
	 */
	public function addCondition($field, $op, $value, $logic_op)
	{
		$this->conditions[] = array("field" => $field, "op" => $op, "value" => $value, "link" => $logic_op);
	}
	/**
	 * 清空查询条件
	 */
	public function clearCondition()
	{
		$this->conditions = array();
	}
	public function clearData()
	{
		$this->data = array();
	}
	/**
	 * 生成sql的where子句
	 */
	protected function parseWhere()
	{
		$where = " WHERE ";
		if(is_array($this->conditions)){
			foreach($this->conditions as $cond)
			{
				$where .= sprintf("`%s` %s '%s' %s ", $cond['field'], $cond['op'], $cond['value'], $cond['link']);
			}
		}else{
			$where .= "0";
		}
		return $where;
	}
	/**
	 * 生成SQL语句
	 */
	public function toSql($type = "select")
	{
		$sql = "";
		if("select" == $type)
		{
			$sql .= "SELECT * FROM `". $this->tableName ."` " . $this->parseWhere();
		}else if("findAll" == $type)
		{
			$sql .= "SELECT * FROM `". $this->tableName ."` ";
		}
		else if("insert" == $type)
		{
			$sql .= "INSERT INTO `". $this->tableName . "`(";
			foreach($this->data as $key => $value)
			{
				$sql .= "`". $key . "`,";
			}
			$sql = substr_replace($sql, ")", strlen($sql) - 1, strlen($sql));
			$sql .= " VALUES(";
			foreach($this->data as $key => $value)
			{
				$sql .= "'" . $value . "',";
			}
			$sql = substr_replace($sql, ")", strlen($sql) - 1, strlen($sql));
		}
		else if("update" == $type)
		{
			$sql .= "UPDATE `". $this->tableName ."` SET ";
			foreach($this->data as $key => $value)
			{
				if(is_int($key))
					continue;
				$sql .= "`" . $key . "` = '" . $value . "',";
			}
			$sql = substr_replace($sql, " ", strlen($sql) - 1, strlen($sql));
			$sql .= $this->parseWhere();
		}
		else if("delete" == $type)
		{
			$sql .= "DELETE FROM `". $this->tableName ."` " . $this->parseWhere();
		}
		else
		{}
		return $sql;
	}
};
?>
